package com.banciyuan.bcywebview.biz.setting;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.google.gson.Gson;

import de.greenrobot.daoexample.model.NightMode;

/**
 * Created by squall on 2017/7/7.
 */

public class NightSettingActivity extends BaseActivity implements View.OnClickListener {

    private ImageView night_contral;
    private View content_line;
    private View start_line;
    private View end_line;
    private TextView start_time;
    private TextView end_time;
    private TimePickerDialog dialog;
    private NightMode nightMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_night_setting);
        initArgs();
        initActionbar();
        initUi();
        initData();
        initAction();
    }


    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.night_mode));
        mActionbarHelper.setActionCallbacks(new ActionbarHelper.OnActionCallbacks() {
            @Override
            public void onBackClick() {
                finish();
            }

            @Override
            public void onActionClick(int id) {

            }
        });
    }

    @Override
    protected void initUi() {
        night_contral = (ImageView) findViewById(R.id.night_iv);
        content_line = findViewById(R.id.content_line);
        start_line = findViewById(R.id.start_time_line);
        end_line = findViewById(R.id.end_time_line);
        start_time = (TextView) findViewById(R.id.start_time_tv);
        end_time = (TextView) findViewById(R.id.end_time_tv);
    }

    @Override
    protected void initData() {
        Gson gson = new Gson();
        String result = SPHelper.getString(this, SPConstant.SPNAME_CLOSELIGHT, SPConstant.All_LIGHT, "{}");
        nightMode = gson.fromJson(result, NightMode.class);
        if (nightMode.is_open()) {
            night_contral.setSelected(true);
            night_contral.setImageResource(R.drawable.switch_on);
            content_line.setVisibility(View.VISIBLE);
        } else {
            night_contral.setSelected(false);
            night_contral.setImageResource(R.drawable.switch_off);
            content_line.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(nightMode.getStart_time())) {
            start_time.setText(nightMode.getStart_time());
        } else {
            start_time.setText("22:00");
            nightMode.setStart_time("22:00");
        }
        if (!TextUtils.isEmpty(nightMode.getEnd_time())) {
            end_time.setText(nightMode.getEnd_time());
        } else {
            end_time.setText("8:00");
            nightMode.setEnd_time("8:00");
        }
    }

    @Override
    protected void initAction() {
        end_line.setOnClickListener(this);
        start_line.setOnClickListener(this);
        night_contral.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.night_iv:
                openNightMode();
                break;
            case R.id.start_time_line:
                selectTime(0);
                break;
            case R.id.end_time_line:
                selectTime(1);
                break;
        }
    }

    private void selectTime(final int from) {
        int hour = 0;
        if (from == 0) {
            hour = Integer.parseInt(nightMode.getStart_time().split(":")[0]);
        } else if (from == 1) {
            hour = Integer.parseInt(nightMode.getEnd_time().split(":")[0]);
        }
        dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                String result = minute >= 10 ? hourOfDay + ":" + minute : hourOfDay + ":" + "0" + minute;
                if (from == 0) {
                    if (!StringUtil.notNullEqual(result, nightMode.getEnd_time())) {
                        nightMode.setStart_time(result);
                    } else {
                        MyToast.show(NightSettingActivity.this, getString(R.string.start_end_same));
                    }
                } else if (from == 1) {
                    if (!StringUtil.notNullEqual(result, nightMode.getStart_time())) {
                        nightMode.setEnd_time(result);
                    } else {
                        MyToast.show(NightSettingActivity.this, getString(R.string.start_end_same));
                    }
                }
                if (!NightSettingActivity.this.isFinishing() && dialog.isShowing()) {
                    dialog.dismiss();
                }
                saveMode();
                addShadow();

            }
        }, hour, 0, true);
        if (!dialog.isShowing())
            dialog.show();
    }

    private void saveMode() {
        if (!TextUtils.isEmpty(nightMode.getStart_time())) {
            start_time.setText(nightMode.getStart_time());
        } else {
            start_time.setText("22:00");
            nightMode.setStart_time("22:00");
        }
        if (!TextUtils.isEmpty(nightMode.getEnd_time())) {
            end_time.setText(nightMode.getEnd_time());
        } else {
            end_time.setText("8:00");
            nightMode.setEnd_time("8:00");
        }

        Gson gson = new Gson();
        String result = gson.toJson(nightMode);
        SPHelper.putString(this, SPConstant.SPNAME_CLOSELIGHT, SPConstant.All_LIGHT, result);
    }

    private void openNightMode() {
        if (night_contral.isSelected()) {
            night_contral.setSelected(false);
            night_contral.setImageResource(R.drawable.switch_off);
            content_line.setVisibility(View.GONE);
            nightMode.setIs_open(false);
        } else {
            night_contral.setSelected(true);
            night_contral.setImageResource(R.drawable.switch_on);
            content_line.setVisibility(View.VISIBLE);
            nightMode.setIs_open(true);
        }
        saveMode();
        if (nightMode.is_open()) {
            addShadow();
        } else {
            removeShadow();
        }
    }

}
